package com.longkier.gx_library_springboot.repository;

import com.longkier.gx_library_springboot.entity.Admin;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface AdminRepository extends JpaRepository<Admin, Long> {

  Optional<Admin> findByUsername(String username);

  @Modifying
  @Query("DELETE FROM Admin a WHERE a.adminId IN :adminId")
  int deleteAdminAllByIdInBatch(@Param("adminId") List<Long> adminId);

  // 新增模糊查询方法
  @Query("SELECT a FROM Admin a " +
          "WHERE (:status IS NULL OR a.status = :status) " +
          "AND (:username IS NULL OR a.username LIKE %:username%) " +
          "AND (:adminName IS NULL OR a.adminName LIKE %:adminName%)")
  Page<Admin> findAdminByCriteria(@Param("status") Integer status,
                                    @Param("username") String username,
                                    @Param("adminName") String adminName,
                                    Pageable pageable);
}
